package com.joymed.dipper

import android.app.Application
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.example.singleactivitytemplate.ui.acts.LiveDataViewModel
import com.example.singleactivitytemplate.ui.acts.LiveDataViewModelBase
import com.example.singleactivitytemplate.ui.acts.SimpleViewModel
import com.example.singleactivitytemplate.ui.acts.SimpleViewModelBase

import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

class MockApplication : Application() {
    // 在这里添加任何必要的模拟行为或属性
}

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun test()
    {
        val timeNodeList = mutableListOf(1,0,4,4,2,1,0,0,0)
        timeNodeList.subList(0,4).distinct().sorted().filter {
            it!=0
        }
        println("the result is:")
    }


}

class SimpleViewModelTest {

    private lateinit var viewModel: SimpleViewModel

    @Before
    fun setUp() {
        viewModel = SimpleViewModel()
    }

    @Test
    fun testModeAdd_WhenModeLessThanMax_ShouldIncrement() {
        // Act
        viewModel.modeAdd()

        // Assert
        assertEquals(2, viewModel.mMode)
    }

    @Test
    fun testModeAdd_WhenModeAtMax_ShouldNotChange() {
        // Arrange
        viewModel.mMode = 2

        // Act
        viewModel.modeAdd()

        // Assert
        assertEquals(2, viewModel.mMode)
    }
}

@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE)
class SimpleViewModelBaseTest {

    private lateinit var viewModel: SimpleViewModelBase

    @Before
    fun setUp() {
        // 初始化 ViewModel 实例
        viewModel = SimpleViewModelBase()
    }

    @Test
    fun testModeAdd_WhenModeLessThanMax_ShouldIncrement() {
        // Arrange
        assertEquals(1, viewModel.mMode)

        // Act
        viewModel.modeAdd()

        // Assert
        assertEquals(2, viewModel.mMode) // 验证模式是否增加到2
    }

    @Test
    fun testModeAdd_WhenModeAtMax_ShouldNotChange() {
        // Arrange
        viewModel.mMode = 2 // 设置模式为最大值2

        // Act
        viewModel.modeAdd()

        // Assert
        assertEquals(2, viewModel.mMode) // 验证模式值没有变化，仍然是2
    }
}

class LiveDataViewModelTest {

    @get:Rule
    var instantTaskExecutorRule = InstantTaskExecutorRule()

    private lateinit var viewModel: LiveDataViewModel

    @Before
    fun setUp() {
        viewModel = LiveDataViewModel()
    }

    @Test
    fun testModeAdd_WhenModeLessThanMax_ShouldIncrement() {
        // Act
        viewModel.modeAdd()

        // Assert
        assertEquals(2, viewModel.mMode.value)
    }

    @Test
    fun testModeAdd_WhenModeAtMax_ShouldNotChange() {
        // Arrange
        viewModel.mMode.value = 2

        // Act
        viewModel.modeAdd()

        // Assert
        assertEquals(2, viewModel.mMode.value)
    }
}

class LiveDataViewModelBaseTest {

    @get:Rule
    var instantTaskExecutorRule = InstantTaskExecutorRule()

    private lateinit var viewModel: LiveDataViewModelBase

    @Before
    fun setUp() {
        // 初始化 ViewModel 实例
        viewModel = LiveDataViewModelBase()
    }

    @Test
    fun testModeAdd_WhenModeLessThanMax_ShouldIncrement() {
        // Act
        viewModel.modeAdd()

        // Assert
        assertEquals(2, viewModel.mMode.value) // 验证模式是否增加到2
    }

    @Test
    fun testModeAdd_WhenModeAtMax_ShouldNotChange() {
        // Arrange
        viewModel.mMode.value = 2 // 设置模式为最大值2

        // Act
        viewModel.modeAdd()

        // Assert
        assertEquals(2, viewModel.mMode.value) // 验证模式值没有变化，仍然是2
    }
}